
# REQUIRES: Data/Roper Data.csv

# NOTE: Uses code from Stargazer Package Updates.R instead of the published version of Stargazer. See readme.txt.

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Roper/Cleaning.R")

################# Table S1 - Predictors of US Public Support for Withdrawal, 1946-2021 #################
# With question and situation features
OLS_qs <- lm(percwithdraw ~  bEnemyVictory + bDeescalationOption + bOnesidedquestion + responseoptions 
             + logcas + FPR + Vietnam + Gulf_War + Afghanistan + Iraq, 
             data = roper)

# With war_duration_months
roper$war_duration_months <- roper$war_duration_days/30.437
OLS_qswd <- lm(percwithdraw ~  war_duration_months
               + bEnemyVictory + bDeescalationOption + bOnesidedquestion + responseoptions 
               + logcas + FPR + Vietnam + Gulf_War + Afghanistan + Iraq, 
               data = roper)

# With  change in Hostile_Cumulative
roper$Hostile_Cumulative_diff <- roper$Hostile_Cumulative-roper$LHostile_Cumulative
roper$plus_Hostile_Cumulative_diff <- roper$Hostile_Cumulative_diff+0.5
roper$log_plus_Hostile_Cumulative_diff <- log(roper$plus_Hostile_Cumulative_diff)
OLS_qshc <- lm(percwithdraw ~  log_plus_Hostile_Cumulative_diff
               + bEnemyVictory + bDeescalationOption + bOnesidedquestion + responseoptions 
               + logcas + FPR + Vietnam + Gulf_War + Afghanistan + Iraq, 
               data = roper)

# With war_duration_months and change in Hostile_Cumulative
OLS_qswdhc <- lm(percwithdraw ~  war_duration_months + log_plus_Hostile_Cumulative_diff
                 + bEnemyVictory + bDeescalationOption + bOnesidedquestion + responseoptions 
                 + logcas + FPR + Vietnam + Gulf_War + Afghanistan + Iraq, 
                 data = roper)

output <- stargazer(OLS_qs, OLS_qswd, OLS_qshc, OLS_qswdhc, title="Table A1: Replication of Observational Results with Duration of War and Logged Change in Casualties",
                    align=TRUE, dep.var.labels=c(""), dep.var.caption="",
                    column.labels=c(" ","","",""),
                    digits = 3, digits.extra = 0,
                    notes = "P-value shown in parentheses below coefficient", 
                    notes.append = FALSE,
                    report = "vc*p",
                    covariate.labels=c("War Duration", "Log Casualties Difference", "Enemy Victory Framing", "Troop Reduction Option", "One-sided Question",
                                       "No. of Response Options", "Log in Change Casualties", "Foreign Policy Restraint",
                                       "Vietnam", "Gulf War", "Afghanistan", "Iraq", "Constant"),
                    omit.stat=c("LL","ser","f","adj.rsq"), no.space=TRUE, notes.align="c", out = "Plots/Table S1 - Predictors of US Public Support for Withdrawal, 1946-2021_R.html")

webshot("Plots/Table S1 - Predictors of US Public Support for Withdrawal, 1946-2021_R.html", "Plots/Table S1 - Predictors of US Public Support for Withdrawal, 1946-2021_R.png", zoom=5, vwidth=500, vheight=720)
# Note: Add the independent variable groups in Word and remove "_R" from the file name
# webshot("Plots/Table S1 - Predictors of US Public Support for Withdrawal, 1946-2021.html", "Plots/Table S1 - Predictors of US Public Support for Withdrawal, 1946-2021.png", zoom=5, vwidth=500, vheight=720)

rm(OLS_qs, OLS_qshc, OLS_qswd, OLS_qswdhc, output)
